//
//  SSSRecordOrderDetailViewController.m
//  SSSMerchant
//
//  Created by 谢曦 on 2017/3/24.
//  Copyright © 2017年 xnxin. All rights reserved.
//

#import "SSSRecordOrderDetailViewController.h"
#import "SSSInfoBaseTableViewCell.h"
#import "NSString+extension.h"

@interface SSSRecordOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
    {
        NSArray *_dataArr;
    }
@property (nonatomic,strong) UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *closeButton;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (nonatomic,strong) UIView *footer;
@property (nonatomic,strong) UIView *selectRatioView;
@end

@implementation SSSRecordOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self tableView];
    self.title=@"录单详情";
    [self configData];
}
    
-(void)configData{
    
  
    
    SSSCellBaseConfigModel *merchantName=[[SSSCellBaseConfigModel alloc]init];
    merchantName.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    merchantName.title=@"商家名称";
    merchantName.identifier=@"name";
    merchantName.rightText=_model.name;
    
    
    SSSCellBaseConfigModel *legalPerson=[[SSSCellBaseConfigModel alloc]init];
    legalPerson.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    legalPerson.title=@"企业法人";
    legalPerson.identifier=@"personName";
    legalPerson.rightText=_model.personName;
    
    SSSCellBaseConfigModel *customerNum=[[SSSCellBaseConfigModel alloc]init];
    customerNum.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    customerNum.title=@"消费者号码";
    customerNum.identifier=@"customerNumber";
    customerNum.rightText=_model.customerNumber;
    
    
    SSSCellBaseConfigModel *customerName=[[SSSCellBaseConfigModel alloc]init];
    customerName.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    customerName.title=@"消费者昵称";
    customerName.identifier=@"customerNickname";
    customerName.rightText=_model.customerNickname;
    
    SSSCellBaseConfigModel *recordType=[[SSSCellBaseConfigModel alloc]init];
    recordType.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    recordType.title=@"录单方式";
    recordType.identifier=@"customerNickname";
    recordType.rightAttributeStr=_model.type==1?[NSString attributedStringWithString:@" 用户上传" andImageName:@"icon_up4" atIndex:0]:[NSString attributedStringWithString:@" 扫码上传" andImageName:@"icon_up3" atIndex:0];
    
    
    SSSCellBaseConfigModel *orderNum=[[SSSCellBaseConfigModel alloc]init];
    orderNum.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    orderNum.title=@"订单号";
    orderNum.identifier=@"orderNumber";
    orderNum.rightText=_model.orderNumber;

    
    
    SSSCellBaseConfigModel *money=[[SSSCellBaseConfigModel alloc]init];
    money.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    money.title=@"消费金额";
    money.identifier=@"money";
    money.rightText=[NSString stringWithFormat:@"¥%@",_model.money];
    
    SSSCellBaseConfigModel *uploadRatio=[[SSSCellBaseConfigModel alloc]init];
    uploadRatio.title=@"上传比例";
    uploadRatio.identifier=@"ratio";
    if (_model.ratio.length>0) {
        uploadRatio.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
        uploadRatio.rightText=_model.ratio;
    }
    else{
        uploadRatio.accessoryType=SSSInfoBaseCellAccessoryTypeCustom;
    }
    
    
    SSSCellBaseConfigModel *uploadMoney=[[SSSCellBaseConfigModel alloc]init];
    uploadMoney.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
    uploadMoney.title=@"上传金额";
    uploadMoney.identifier=@"uploadMoney";
    uploadMoney.rightText=_model.uploadMoney;
    
    
    _dataArr=@[@[merchantName,legalPerson],@[customerNum,customerName,recordType],@[orderNum,money,uploadRatio,uploadMoney]];
    [self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_dataArr[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SSSInfoBaseTableViewCell *cell=[SSSInfoBaseTableViewCell cellForTableView:tableView withIdentifier:@"SSSInfoBaseTableViewCell"];
   
  
    if (indexPath.section==2&&indexPath.row==2) {
        cell.customAccessoryView=self.selectRatioView;
        
    }
     cell.configModel=_dataArr[indexPath.section][indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 51;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
      return section==2?220:0.1;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
        return section==2?self.footer:[UIView new];
    }
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==2&&indexPath.row==2) {
        [self selectRatio];
    }

}
#pragma mark - action
- (IBAction)saveButtonClick:(id)sender {
    NSDictionary *param=[_model  mj_keyValues];
    NSLog(@"%@",param);
}
- (IBAction)closeButtonClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)selectRatio{
    XXSheetView *sheet=[XXSheetView sheetViewWithTitle:@"选择上传比例" redButtonTitle:nil otherbuttonTitle:@[@"5%",@"10%",@"15%"]];
    sheet.defaultTextCancel=@"暂不设置";
    sheet.sheetViewRedButtonClickBlock=^{
        
    };
    NSIndexPath *index=[NSIndexPath indexPathForRow:2 inSection:2];
  SSSInfoBaseTableViewCell *cell=  [self.tableView cellForRowAtIndexPath:index];
    SSSCellBaseConfigModel *model=cell.configModel;
    sheet.sheetViewOtherButtonClickBlock=^(NSUInteger index,NSString *title){
        model.rightText=title;
        model.accessoryType=SSSInfoBaseCellAccessoryTypeLable;
        _model.ratio=title;
        cell.configModel=model;
        
           };
    [sheet showSheetView];
    
    
}
    
-(UIView *)selectRatioView{
    if (!_selectRatioView) {
        
    
   _selectRatioView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 51)];
    
    UIImageView *downPic= [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_arrows_down"]] ;
     [_selectRatioView addSubview:downPic];
   
    [downPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_selectRatioView);
        make.right.equalTo(_selectRatioView);
    }];

    
    UILabel *lable= [UILabel new] ;
    lable.text=@"请选择";
        lable.tag=100;
    [lable sizeToFit];
    [_selectRatioView addSubview:lable];
    lable.font=Font(14);
    lable.textColor=[UIColor colorWithHex:@"BBBBBB"];;
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(_selectRatioView);
        make.right.equalTo(downPic.mas_left).mas_equalTo(-7);
    }];
    }
    return _selectRatioView;

}
-(UIView *)footer{
    if (!_footer) {
       _footer=   [[NSBundle mainBundle] loadNibNamed:@"SSSRecordDetailFooter" owner:self options:nil].firstObject;
        _closeButton.layer.cornerRadius=4;
        _closeButton.layer.borderWidth=1;
        _closeButton.layer.borderColor=ThemeGreenColor.CGColor;
        _saveButton.layer.cornerRadius=4;

    }
     return _footer;
}
    
-(UITableView *)tableView
    {
        if (!_tableView) {
            _tableView=[[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
            _tableView.backgroundColor=[UIColor colorWithHex:@"F6F6F6"];
            _tableView.delegate=self;
            _tableView.dataSource=self;
//            _tableView.tableFooterView=[UIView new];
//            _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
            [self.view addSubview:_tableView];
        }
        
        return _tableView;
        
    }

@end
